package com.cykj.enterprise.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.cykj.common.dto.ResponseDTO;
import com.cykj.common.entity.HrDO;
import com.cykj.common.entity.JobDO;
import com.cykj.common.entity.JobTypeDO;
import com.cykj.common.entity.RecruitInfoDO;
import com.cykj.enterprise.vo.HrVO;
import com.cykj.enterprise.mapper.RecruitMapper;
import com.cykj.enterprise.service.IRecruitService;
import com.cykj.enterprise.vo.RecruitInfoVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @BelongsProject: smart_recruit_backend
 * @BelongsPackage: com.cykj.enterprise.service.impl
 * @Author: 小白糖糖
 * @CreateTime: 2025-08-18 22:12
 * @Description: TODO
 * @Version: 1.0
 */
@Service
public class RecruitServiceImpl implements IRecruitService {
    @Autowired
    private RecruitMapper recruitMapper;

    @Override
    public ResponseDTO getHrInfo() {
        HrDO hrDO = recruitMapper.getHrInfo(StpUtil.getLoginIdAsInt());
        if (hrDO.getCompanyId() == null) {
            return ResponseDTO.error("还未通过企业资质审核！请耐心等待！");
        }
        return ResponseDTO.success(hrDO);
    }

    @Override
    public ResponseDTO getRecruitList(HrVO hrVO) {
        List<RecruitInfoDO> recruitList;
        hrVO.setHrId(StpUtil.getLoginIdAsInt());
        if (hrVO.getPageSize() == null) {
            return ResponseDTO.success(recruitMapper.getRecruitList(hrVO));
        }
        PageHelper.startPage(hrVO.getPageNum(), hrVO.getPageSize());
        recruitList = recruitMapper.getRecruitList(hrVO);
        PageInfo<RecruitInfoDO> recruitInfoDOPageInfo = new PageInfo<>(recruitList);
        int total = (int) recruitInfoDOPageInfo.getTotal();
        if (total != 0 && recruitList.isEmpty()) {
            hrVO.setPageNum(1);
            PageHelper.startPage(hrVO.getPageNum(), hrVO.getPageSize());
            recruitList = recruitMapper.getRecruitList(hrVO);
            recruitInfoDOPageInfo = new PageInfo<>(recruitList);
        }
        return ResponseDTO.success(recruitInfoDOPageInfo);
    }

    @Override
    public ResponseDTO getJobTypeList() {
        List<JobTypeDO> jobTypeList = recruitMapper.getJobTypeList();
        return ResponseDTO.success(jobTypeList);
    }

    @Override
    public ResponseDTO getSpecificJobList(Integer typeId) {
        List<JobDO> specificJobList = recruitMapper.getSpecificJobList(typeId);
        return ResponseDTO.success(specificJobList);
    }

    @Override
    public ResponseDTO addRecruit(RecruitInfoVO recruitInfoVO) {
        RecruitInfoDO recruitInfoDO = recruitMapper.getRecruitByHrIdAndCompanyId(StpUtil.getLoginIdAsInt(), recruitInfoVO.getJobId());
        if (recruitInfoDO == null){
            Integer i = recruitMapper.addRecruit(recruitInfoVO);
            if(i > 0){
                return ResponseDTO.success("发布成功！请耐心等待审核");
            }
            return ResponseDTO.error("未知错误！");
        }
        return ResponseDTO.error("您已发布过相同职位的招聘！");
    }

    @Override
    public ResponseDTO updateRecruit(RecruitInfoVO recruitInfoVO) {
        Integer i = recruitMapper.updateRecruit(recruitInfoVO);
        if (i > 0){
            return ResponseDTO.success("修改成功！请等待审核！");
        }
        return ResponseDTO.error("修改失败！");
    }

    @Override
    public ResponseDTO identityCheck(HrVO hrVO) {
        hrVO.setHrId(StpUtil.getLoginIdAsInt());

        HrDO hrDO = recruitMapper.identityCheck(hrVO);
        if (hrDO != null){
            return ResponseDTO.success();
        }
        return ResponseDTO.error("身份认证失败！");
    }

    @Override
    public ResponseDTO deleteRecruit(Integer recruitId) {
        Integer i = recruitMapper.deleteRecruit(recruitId);
        if(i > 0){
            return ResponseDTO.success("招聘信息删除成功！");
        }
        return ResponseDTO.error("招聘信息删除失败！");
    }
}
